﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace Noris.Tools.TraceVisualiser.Forms
{
    /// <summary>
    /// Status bar s pevnou konstrukcí a s přístupovou metodou IFormStatusbar.StatusBarSet(item, value)
    /// </summary>
	internal class StatusBar : StatusStrip, IFormStatusbar
    {
        #region Konstrukce
        public StatusBar()
		{
			this._Init();
		}
		private void _Init()
		{
			this._Icon = new ToolStripLabel();
            this._Icon.Alignment = ToolStripItemAlignment.Left;
            this._Icon.AutoSize = false;
            this._Icon.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this._Icon.ImageScaling = ToolStripItemImageScaling.SizeToFit;
            this._Icon.Width = 23;

            this._Version = new ToolStripLabel();
            this._Version.Alignment = ToolStripItemAlignment.Left;
            this._Version.AutoSize = false;
            this._Version.TextAlign = ContentAlignment.MiddleLeft;
            this._Version.Width = 150;

            this._MainText = new ToolStripLabel();
            this._MainText.Alignment = ToolStripItemAlignment.Left;
            this._MainText.TextAlign = ContentAlignment.MiddleLeft;
            this._MainText.AutoSize = false;
            this._MainText.ImageAlign = ContentAlignment.MiddleLeft;
            this._MainText.TextImageRelation = TextImageRelation.ImageBeforeText;
            this._MainText.Width = 380;

            this._StatusText = new ToolStripLabel();
            this._StatusText.Alignment = ToolStripItemAlignment.Right;
            this._StatusText.TextAlign = ContentAlignment.MiddleLeft;
            this._StatusText.AutoSize = false;
            this._StatusText.ImageAlign = ContentAlignment.MiddleLeft;
            this._StatusText.TextImageRelation = TextImageRelation.ImageBeforeText;
            this._StatusText.Width = 200;

            this._MemoryText = new ToolStripLabel();
            this._MemoryText.Alignment = ToolStripItemAlignment.Right;
            this._MemoryText.TextAlign = ContentAlignment.MiddleLeft;
            this._MemoryText.AutoSize = false;
            this._MemoryText.Image = Pics.PicLibrary16.MemoryRom;
            this._MemoryText.ImageAlign = ContentAlignment.MiddleLeft;
            this._MemoryText.TextImageRelation = TextImageRelation.ImageBeforeText;
            this._MemoryText.Width = 175;
            this._MemoryText.Text = "Memory";

            this._ProgrSep = new ToolStripSeparator();
            this._ProgrSep.Visible = false;

            this._Progress = new ToolStripProgressBar();
            this._Progress.Alignment = ToolStripItemAlignment.Right;
            this._Progress.Minimum = 0;
            this._Progress.Maximum = 250;
            this._Progress.Style = ProgressBarStyle.Continuous;
            this._Progress.Height = 14;
            this._Progress.Value = 0;
            this._Progress.Visible = false;
            this._Progress.Width = 120;

			this.Items.Add(this._Icon);
            this.Items.Add(this._Version);
            this.Items.Add(new ToolStripSeparator());
            this.Items.Add(this._MainText);
            this.Items.Add(new ToolStripSeparator());
            this.Items.Add(this._StatusText);
            this.Items.Add(new ToolStripSeparator());
            this.Items.Add(this._MemoryText);
            this.Items.Add(this._ProgrSep);
            this.Items.Add(this._Progress);

            this.GripStyle = ToolStripGripStyle.Visible;
            this.AllowItemReorder = false;
            this.RenderMode = ToolStripRenderMode.Professional;
            this.SizingGrip = true;
            this.Stretch = true;
            this.SizeChanged += new EventHandler(StatusBar_SizeChanged);
        }

        void StatusBar_SizeChanged(object sender, EventArgs e)
        {
            this._ResizeMainText();
        }
        private void _ResizeMainText()
        {
            int progressWidth = (this._Progress.Visible ? this._Progress.Width : 0);
            this._MainText.Width = this.Width - this._Icon.Width - this._Version.Width - this._StatusText.Width - this._MemoryText.Width - progressWidth - 50;
        }
		private ToolStripLabel _Icon;
        private ToolStripLabel _Version;
        private ToolStripLabel _MainText;
        private ToolStripLabel _StatusText;
        private ToolStripLabel _MemoryText;
        private ToolStripSeparator _ProgrSep;
		private ToolStripProgressBar _Progress;
        #endregion
        #region IFormStatusbar Members

        void IFormStatusbar.StatusBarSet(StatusBarItem item, object value)
        {
            if (this.InvokeRequired)
                this.BeginInvoke(new Action<StatusBarItem, object>(_StatusBarSet), item, value);
            else
                this._StatusBarSet(item, value);
        }

        private void _StatusBarSet(StatusBarItem item, object value)
        {
            switch (item)
            {
                case StatusBarItem.Icon:
                    if (value is Image)
                        this._Icon.Image = value as Image;
                    break;

                case StatusBarItem.Version:
                    if (value is string)
                        this._Version.Text = value as string;
                    break;

                case StatusBarItem.MainText:
                    if (value == null)
                    {
                        this._MainText.Text = "";
                        this._MainText.Image = null;
                    }
                    else if (value is string)
                        this._MainText.Text = value as string;
                    else if (value is Image)
                        this._MemoryText.Image = value as Image;
                    break;

                case StatusBarItem.StatusText:
                    if (value == null)
                    {
                        this._StatusText.Text = "";
                        this._StatusText.Image = null;
                    }
                    else if (value is string)
                        this._StatusText.Text = value as string;
                    else if (value is Image)
                        this._StatusText.Image = value as Image;
                    break;

                case StatusBarItem.MemoryText:
                    if (value is string)
                        this._MemoryText.Text = value as string;
                    break;

                case StatusBarItem.Progress:
                    this._StatusBarSetProgress(value);
                    break;

            }
        }

        private void _StatusBarSetProgress(object value)
        {
            if (value == null)
            {
                if (this._Progress.Visible)
                {
                    this._ProgrSep.Visible = false;
                    this._Progress.Visible = false;
                    this._Progress.Value = 0;
                    this._ResizeMainText();
                }
            }
            else if (value is decimal)
                _StatusBarSetProgress((decimal)value);
            else if (value is float)
                _StatusBarSetProgress((decimal)value);
            else if (value is int)
                _StatusBarSetProgress(((decimal)value) / 100M);
        }
        private void _StatusBarSetProgress(decimal ratio)
        {
            decimal r = (ratio < 0M ? 0M : (ratio > 1M ? 1M : ratio));
            decimal value = ((decimal)this._Progress.Maximum) * r;
            this._Progress.Value = (int)(Math.Round(value, 0));
            if (!this._Progress.Visible)
            {
                this._ProgrSep.Visible = true;
                this._Progress.Visible = true;
                this._ResizeMainText();
            }
        }

        #endregion
    }
}
